(function() {
	/***************************************************************************
	 * 主页面维护
	 */
	var centerGridView = {
		fnShop: function() {
			var result = methods.getShop();
			var form = myShop;
			if(undefined == result) {
				form.uv_shopId(platform.uuid());
				form.uv_shopPerson(methods.getUser().user_id);
				form.uv_shopName('');
				form.uv_address('');
				form.uv_info('');
				form.uv_tel('');
				form.uv_email('');
				var oFileInput = new FileInput();
				//上传图片控件 初始化
				oFileInput.Init("uv_shopPermit", platform.mapUrl('~/rest/shop/save'));
			} else {
				form.uv_shopId(result.shop_id);
				form.uv_shopPerson(result.shop_person);
				form.uv_shopName(result.shop_name);
				form.uv_address(result.shop_address);
				form.uv_info(result.shop_info);
				form.uv_tel(result.shop_contact);
				form.uv_email(result.shop_logo);
				var oFileInput = new FileInput();
				//上传图片控件 初始化
				oFileInput.Init("uv_shopPermit", platform.mapUrl('~/rest/shop/save'));
			}
			$('#myShop').modal('show');
		},
		fnPwd: function() {
			var result = platform.getCurrentUser();
			var form = myPwd;
			form.uv_userPwd(result.user_pwd);
			form.uv_modifyPwd('');
			form.uv_confirmPwd('');
			$('#myPwd').modal('show');
		},
		fnPay: function() {
			var shop_person = platform.getCurrentUser().user_id;
			var data_status = "2";
			grantPayView.init(shop_person, data_status);
			$('#grantPayView').modal('show');
		}
	};
	ko.applyBindings(centerGridView, document.getElementById('centerGridView'));
	/***************************************************************************
	 * 店铺模态框数据初始化
	 */
	var myShop = {
		uv_shopId: ko.observable(''),
		uv_shopName: ko.observable(''),
		uv_address: ko.observable(''),
		uv_shopPerson: ko.observable(''),
		uv_info: ko.observable(''),
		uv_tel: ko.observable(''),
		uv_tel: ko.observable(''),
		uv_email: ko.observable(''),
		fnShopSave: function() {
			$('#uv_shopPermit').fileinput('upload');
		}
	};
	ko.applyBindings(myShop, document.getElementById('myShop'));
	/*
	 * 密码模块维护
	 */
	var myPwd = {
		uv_userPwd: ko.observable(''),
		uv_modifyPwd: ko.observable(''),
		uv_confirmPwd: ko.observable(''),
		clear: function() {
			this.uv_userPwd('');
			this.uv_modifyPwd('');
			this.uv_confirmPwd('');
		},
		fnPwdSave: function() {
			var form = myPwd;
			var param = {};
			if(form.uv_modifyPwd() != null && form.uv_confirmPwd() != null && form.uv_modifyPwd() == form.uv_confirmPwd()) {
				param.user_id = methods.getUser().user_id;
				param.user_name = methods.getUser().user_name;
				param.user_pwd = CryptoJS.enc.Base64.stringify(CryptoJS.MD5(form
					.uv_modifyPwd()));
				param.edit_status = 2;
				methods.updateUserPwd(param)

			}
		}
	};
	ko.applyBindings(myPwd, document.getElementById('myPwd'));
	var grantPayView = {
		init: function(shop_person, data_status) {
			var columns = [{
				checkbox: true,
				field: 'checkStatus',
				align: 'center',
				valign: 'middle',
			}, {
				field: 'receive_id',
				title: '主键标识',
				width: 60,
				valign: 'middle',
				align: 'center',
				visible: false
			}, {
				field: 'receive_way',
				title: '支付方式',
				width: 120,
				valign: 'middle'
			}, {
				field: 'receive_qrocode',
				title: '支付帐号',
				width: 180,
				valign: 'middle'
			}];
			$('#grantedPayGrid').bootstrapTable({
				url: platform.mapUrl('~/rest/receive/selectByPerson'),
				cache: false,
				method: 'post',
				contentType: 'application/json',
				dataType: 'json',
				search: false,
				strictSearch: false,
				singleSelect: false,
				striped: true,
				showFooter: false,
				showToggle: false,
				// showRefresh : true,
				showPaginationSwitch: false,
				silent: true,
				pagination: false,
				sidePagination: 'server',
				queryParams: function(params) {
					return {
						shop_person: shop_person,
						data_status: data_status
					}
				},
				clickToSelect: true,
				height: 275,
				uniqueId: 'receive_id',
				detailView: false,
				cardView: false,
				toolbar: '#toolbar',
				columns: columns,
			});
		},
		fnAdd: function() {
			myPayAdd.clear();
			$('#myPayAdd').modal('show');
		},
		fnRemove: function() {}
	};
	ko.applyBindings(grantPayView, document.getElementById('grantPayView'));
	var myPayAdd = {
		uv_Pay: ko.observable(''),
		uv_numberPay: ko.observable(''),
		clear: function() {
			this.uv_Pay('');
			this.uv_numberPay('');
		},
		fnPaySave: function() {
			var params = {};
			params.receive_id = platform.uuid();
			params.receive_way = myPayAdd.uv_Pay();
			params.receive_qrocode = myPayAdd.uv_numberPay();
			params.data_status = 2;
			methods.savePay(params);
		}
	};
	ko.applyBindings(myPayAdd, document.getElementById('myPayAdd'));
	var FileInput = function() {
        var oFile = {};

		// 初始化fileinput控件（第一次初始化）
		oFile.Init = function(ctrlName, uploadUrl) {
			var control = $('#' + ctrlName);

			// 初始化上传控件的样式
			control.fileinput({
				language: 'zh', // 设置语言
				uploadUrl: uploadUrl, // 上传的地址
				allowedFileExtensions: ['jpg', 'gif', 'png'], // 接收的文件后缀
				showUpload: false, // 是否显示上传按钮
				showCaption: false, // 是否显示标题
				showUploadedThumbs: false,
				browseClass: "btn btn-primary", // 按钮样式
				uploadAsync: true,
				autoReplace: true,
				overwriteInitial: true,
				// dropZoneEnabled: false,//是否显示拖拽区域
				// minImageWidth: 50, //图片的最小宽度
				// minImageHeight: 50,//图片的最小高度
				// maxImageWidth: 1000,//图片的最大宽度
				// maxImageHeight: 1000,//图片的最大高度
				// maxFileSize: 0,//单位为kb，如果为0表示不限制文件大小
				// minFileCount: 0,
				maxFileCount: 3, // 表示允许同时上传的最大文件个数
				enctype: 'multipart/form-data',
				validateInitialCount: true,
				previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
				msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
				fileActionSettings: {
					showUpload: false,
					showRemove: false
				},
				uploadExtraData: function(previewId, index) {
					var param = {};
					param.shop_id = document.getElementById('shopId').value;
					param.shop_name = document.getElementById('shopName').value;
                    param.shop_address = document.getElementById("shopAddress").value;
					param.shop_info = document.getElementById('shopInfo').value;
					param.shop_contact = document.getElementById('shopTel').value;
					param.shop_logo = document.getElementById('shopEmail').value;
					param.shop_person = document.getElementById('shopPerson').value;
					return param;
				}
			});
        };
		return oFile;
	};
	var methods = {
		getShop: function() {
			var param = {};
			param.shop_person = platform.getCurrentUser().user_id;
			var result = JSON2.parse(platform.servicebreak(
				'~/rest/shop/selectOne', param).responseText);
			return result;
		},
		updateUserPwd: function(param) {
			var result = JSON2.parse(platform.servicebreak(
				'~/rest/user/updateUserPwd', param).responseText);
			return result;
		},

		saveShop: function() {
			var param = {};
			param.shop_person = platform.getCurrentUser().user_id;
			param.shop_id = form.uv_shopId();
			param.shop_name = form.uv_shopName();
			param.shop_contact = form.uv_tel();
			var result = JSON2.parse(platform.servicebreak('~/rest/shop/save',
				param).responseText);
			return result;
			$('#myShop').modal('hidden');
			platform.showOperTip('result');
		},
		getUser: function() {
			var param = {};
			param.user_id = platform.getCurrentUser().user_id;
			var result = JSON2.parse(platform.servicebreak(
				'~/rest/user/selectOne', param).responseText).result;
			return result;
		},
		getPay: function() {
			var param = {};
			param.shop_person = platform.getCurrentUser().user_id;
			var result = JSON2.parse(platform.servicebreak(
				'~/rest/receive/selectOne', param).responseText).result;
			return result;
		},
		savePay: function(param) {
			param.shop_person = platform.getCurrentUser().user_id;
			var result = JSON2.parse(platform.servicebreak(
				'~/rest/receive/save', param).responseText).result;
			return result;
		}
	}
})(this);